package sk.tuke.android4all.dialer;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;

public class ContactsAdapter {
	private static String TAG = "ContactsAdapter";
	private Context mContext = null;
    private ContactInfo[] mContainer = null;
    private List<ContactInfo> mAllContacts = null;
    private final int ITEMS_PER_SCREEN = 5;
    private int index = 0;

    public ContactsAdapter(Context mContext_in){
        this.mContext = mContext_in;
        mContainer = new ContactInfo[ITEMS_PER_SCREEN];
        mAllContacts = new ArrayList<ContactInfo>();
        getAllContacts();
    }

    public ContactInfo[] getCurrentSet(){
    	if(mAllContacts.size() < ITEMS_PER_SCREEN){
    		 for(int i = 0; i<ITEMS_PER_SCREEN; i++){
 	        	try{
 	        		mContainer[i] = mAllContacts.get(i);
 	        	}catch(Exception ex){
 	        		mContainer[i] = null;
 	        	}
 	        	
 	        }
    	}else {
	        for(int i = 0; i<ITEMS_PER_SCREEN; i++){
	        	if(i+index >= mAllContacts.size()){
	        		index = -i;
	        	}
	        	try{
	        		mContainer[i] = mAllContacts.get(index +i);
	        	}catch(Exception ex){
	        		mContainer[i] = null;
	        	}
	        	
	        }
    	}
        return mContainer;
    }
    
 
    public void ScrollDown(){
    	index+=ITEMS_PER_SCREEN;
    }
    public boolean scrollNeeded(){
    	return ITEMS_PER_SCREEN<mAllContacts.size();
    }
    public boolean isEmpty(){
    	return mAllContacts.size()==0;
    }
    
    private void getAllContacts() {
        Log.d(TAG,"GetAllPhoneContacts");  
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        Cursor cursor = mContext.getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        cursor.moveToFirst();
        while (!cursor.isAfterLast())
        {
            String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
            String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));

            ContactInfo aContact = new ContactInfo();  
            aContact.setName(contactName);            
            aContact.setNumber(contactNumber);                 
            aContact.setId(String.valueOf(id));
            
            mAllContacts.add(aContact);
            cursor.moveToNext();
        }       
        cursor.close();
        cursor = null;
    }
    
    public ContactInfo getContactInfo(String id){
    	ContactInfo aContact = null;
    	for(ContactInfo i: mAllContacts){
    		if(i.getId() == id)
    			aContact = i;
    	}
    	return aContact;  	
    }
}
